package com.example.njut;

import androidx.appcompat.app.AppCompatActivity;

import android.content.ContentValues;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.text.InputType;
import android.view.View;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.TextView;
import android.widget.Toast;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class ManagerActivity extends AppCompatActivity {
    private EditText inputName;
    private EditText inputPwd;
    private Button showPwd;
    private EditText inputAge;
    private Button select;
    private Button add;
    private Button delete;
    private Button update;
    private ListView lv;

    private int i=0;

    dbHelper dbHelper;
    String DB_Name="mydb";
    SQLiteDatabase db;
    Cursor cursor;
    ContentValues selCV;

    private ArrayList<Map<String,Object>> data;
    private Map<String,Object> item;
    private SimpleAdapter listAdapter;
    View view;

    String selId;
    boolean flag=true;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_manager);

        inputName = (EditText) findViewById(R.id.inputName);
        inputPwd = (EditText) findViewById(R.id.inputPwd);
        showPwd = (Button) findViewById(R.id.button3);
        inputAge = (EditText) findViewById(R.id.editText7);
        select = (Button) findViewById(R.id.button9);
        add = (Button) findViewById(R.id.button10);
        delete = (Button) findViewById(R.id.button11);
        update = (Button) findViewById(R.id.button12);
        lv = (ListView) findViewById(R.id.lv);

        dbHelper = new dbHelper(this,DB_Name,null,1);
        db = dbHelper.getWritableDatabase();
        data = new ArrayList<Map<String, Object>>();

        dbFindAll();

        showPwd.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(i==0){
                    inputPwd.setInputType(128);
                    showPwd.setText("隐藏密码");
                    i = 1;
                }else{
                    inputPwd.setInputType(129);
                    showPwd.setText("显示密码");
                    i = 0;
                }
            }
        });
        select.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                inputName.setText("");
                inputPwd.setText("");
                inputAge.setText("");
                delete.setEnabled(false);
                update.setEnabled(false);
                inputName.setEnabled(true);
                dbFindAll();
            }
        });

        add.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                dbAdd();
                dbFindAll();
            }
        });

        delete.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                dbDelete();
                dbFindAll();
                inputName.setText("");
                inputPwd.setText("");
                inputAge.setText("");
                delete.setEnabled(false);
                update.setEnabled(false);
                inputName.setEnabled(true);
                Toast.makeText(ManagerActivity.this,"删除成功!",Toast.LENGTH_SHORT).show();
            }
        });

        update.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                dbUpdate();
                dbFindAll();
                Toast.makeText(ManagerActivity.this,"修改成功!",Toast.LENGTH_SHORT).show();
            }
        });
        //修改代码，实现界面显示连贯序号
        lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                    delete.setEnabled(true);
                    update.setEnabled(true);
                    inputName.setEnabled(false);
                    Map<String,Object>listItem = (Map<String,Object>)lv.getItemAtPosition(i);
                    inputName.setText((String)listItem.get("uname"));
                    inputPwd.setText((String)listItem.get("pwd"));
                    inputAge.setText((String)listItem.get("age"));
                    selId = (String)listItem.get("uname");
            }
        });
    }

    private void dbUpdate(){
        String p = inputPwd.getText().toString();
        String a = inputAge.getText().toString();
        if(p!=null&&!p.equals("") && a!=null&&!a.equals("")){
            selCV = new ContentValues();
            //selCV.put("uname",inputName.getText().toString());
            selCV.put("pwd",p);
            selCV.put("age",a);
            //更新条件
            String whereClause = "uname=?";
            //更新条件的值
            String[] whereArgs={String.valueOf(selId)};
            db.update(dbHelper.TB_Name,selCV,whereClause,whereArgs);
            inputName.setText("");
            inputPwd.setText("");
            inputAge.setText("");
            delete.setEnabled(false);
            update.setEnabled(false);
            inputName.setEnabled(true);
        }else{
            Toast.makeText(ManagerActivity.this,"未输入完全",Toast.LENGTH_SHORT).show();
        }
    }

    private void dbDelete(){
        //删除条件
        String whereClause = "uname=?";
        //删除的值
        String[] whereArgs={String.valueOf(selId)};
        db.delete(dbHelper.TB_Name,whereClause,whereArgs);
    }

    private void dbAdd(){
        selCV = new ContentValues();
        cursor = db.query(dbHelper.TB_Name,null,null,null,null,null,"uid ASC");
        cursor.moveToFirst();
        while(!cursor.isAfterLast()){
            if(inputName.getText().toString().trim().equals(cursor.getString(1))){
                flag = false;
            }
            cursor.moveToNext();
        }
        if(flag){
            String n = inputName.getText().toString();
            String p = inputPwd.getText().toString();
            String a = inputAge.getText().toString();
            if(n!=null&&!n.equals("") && p!=null&&!p.equals("") && a!=null&&!a.equals("")){
                selCV.put("uname",n);
                selCV.put("pwd",p);
                selCV.put("age",a);
                long rowId = db.insert(dbHelper.TB_Name,null,selCV);
                if(rowId==-1){
                    Toast.makeText(ManagerActivity.this,"发生未知错误，添加失败",Toast.LENGTH_SHORT).show();
                }else {
                    Toast.makeText(ManagerActivity.this, "添加成功！", Toast.LENGTH_SHORT).show();
                }
                inputName.setText("");
                inputPwd.setText("");
                inputAge.setText("");
            }else{
                Toast.makeText(ManagerActivity.this,"未输入完全",Toast.LENGTH_SHORT).show();
            }
        }else{
            Toast.makeText(ManagerActivity.this,"用户已存在",Toast.LENGTH_SHORT).show();
            flag = true;
        }
    }

    private void dbFindAll(){
        int num=1;
        data.clear();
        cursor = db.query(dbHelper.TB_Name,null,null,null,null,null,null);
        cursor.moveToFirst();
        while(!cursor.isAfterLast()){

            String uid = cursor.getString(0);
            String name = cursor.getString(1);
            String pwd = cursor.getString(2);
            String age = cursor.getString(3);
            item = new HashMap<String, Object>();
            item.put("uid",num);
            item.put("uname",name);
            item.put("pwd",pwd);
            item.put("age",age);
            data.add(item);
            num++;
            cursor.moveToNext();
        }
        showList();
    }

    public void showList(){
        listAdapter = new SimpleAdapter(this,data,
                R.layout.sqllist,
                new String[]{"uid","uname","pwd","age"},
                new int[]{R.id.textView14,R.id.textView19,R.id.textView20,R.id.textView21});
        lv.setAdapter(listAdapter);
    }
}